<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
<!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="query">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>



    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column prop="serialNumber" label="批次号" width="400" align="center"></el-table-column>
      <el-table-column prop="executionStatus" label="运算状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag size="big" :type="scope.row.executionStatus === 1 ? 'success'
          : scope.row.executionStatus === 0 ? 'primary'
          : scope.row.executionStatus === -1 ? 'danger'
          : 'warning'">{{ scope.row.executionStatus === 1 ? '成功'
              : scope.row.executionStatus === 0 ? '运行中'
                  : scope.row.executionStatus === -1 ? '失败' : '未运行' }}<i v-if="scope.row.executionStatus === 0" class="el-icon-loading"></i></el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saveStatus" label="保存状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag size="big" :type="scope.row.saveStatus === 1 ? 'success'
          : scope.row.saveStatus === 0 ? 'primary'
          : scope.row.saveStatus === -1 ? 'danger'
          : 'warning'">{{ scope.row.saveStatus === 1 ? '成功'
              : scope.row.saveStatus === 0 ? '运行中'
                  : scope.row.saveStatus === -1 ? '失败' : '未运行' }}<i v-if="scope.row.saveStatus === 0" class="el-icon-loading"></i> </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="executionProgress" label="运算进度" width="200">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="26" :status="scope.row.saveProgress === 100 ? 'success' :'warning'" :percentage="scope.row.executionProgress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column prop="saveProgress" label="保存进度" width="200" align="center">
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="26" :status="scope.row.saveProgress === 100 ? 'success' :'warning'" :percentage="scope.row.saveProgress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column prop="executionCreateTime" label="运算起始" width="90" align="center"></el-table-column>
      <el-table-column prop="executionEndTime" label="运算结束" width="90" align="center"></el-table-column>
      <el-table-column prop="executionTime" label="运算耗时" width="80" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.executionStatus === 1 ? 'success'
          : scope.row.executionStatus === 0 ? 'primary'
          : scope.row.executionStatus === -1 ? 'danger' : 'warning'">{{ scope.row.executionTime}}秒</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="saveStartTime" label="数据库保存开始时间" width="90" align="center"></el-table-column>
      <el-table-column prop="savaEndTime" label="数据库保存结束时间" width="90" align="center"></el-table-column>
      <el-table-column prop="saveTime" label="数据库保存耗时" width="80" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.saveStatus === 1 ? 'success'
          : scope.row.saveStatus === 0 ? 'primary'
          : scope.row.saveStatus === -1 ? 'danger' : 'warning'">{{ scope.row.saveTime}}秒</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operatorName" label="操作人"></el-table-column>


      <el-table-column label="操作"  width="120" align="center" fixed="right">
        <template slot-scope="scope">
<!--          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>-->
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
      <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
        <el-form-item label="开始运算时间">
          <el-date-picker v-model="form.executionCreateTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束运算时间时间">
          <el-date-picker v-model="form.executionEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="运算进度">
          <el-input v-model="form.executionProgress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="运算状态">
          <el-input v-model="form.executionStatus" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="运算耗时">
          <el-date-picker v-model="form.executionTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="操作人">
          <el-input v-model="form.operatorName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据库保存结束时间">
          <el-date-picker v-model="form.savaEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="数据库保存进度">
          <el-input v-model="form.saveProgress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据库保存开始时间">
          <el-date-picker v-model="form.saveStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="保存状态">
          <el-input v-model="form.saveStatus" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据库保存耗时">
          <el-date-picker v-model="form.saveTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="批次号">
          <el-input v-model="form.serialNumber" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CompareTask",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      uploadHeaders: {"token": localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).token : ''},
      queryInterval: null, // 用于存储定时器ID
    }
  },
  created() {
    this.load()
    this.loopQueryInterface()
  },
  beforeDestroy() {
    // 清除定时器
    clearInterval(this.queryInterval);
  },
  methods: {
    // 写一个定时器循环请求接口
    loopQueryInterface() {
      this.queryInterval = setInterval(() => {
        this.load();
      }, 2000);
    },
    load() {
      this.request.get("/compareTask/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    reset() {
      this.pageNum = 1
      this.pageSize = 10
      this.name = ""
      this.load()
    },
    save() {
      this.request.post("/compareTask", this.form).then(res => {
        if (res.code === '0') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
       this.$nextTick(() => {
         if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
       })
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    del(id) {
      this.request.delete("/compareTask/" + id).then(res => {
        if (res.code === '0') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/compareTask/del/batch", ids).then(res => {
        if (res.code === '0') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    download(url) {
      window.open(url)
    },
    query(){
      this.pageNum = 1
      this.load()
    },
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>
